package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.pojo.bo.system.RoleBO;
import com.lwc.cfdns.pojo.dto.ChangeStatusDTO;
import com.lwc.cfdns.pojo.dto.system.RoleAuthUserDTO;
import com.lwc.cfdns.pojo.dto.system.RoleDTO;
import com.lwc.cfdns.pojo.dto.system.RoleDataScopeDTO;
import com.lwc.cfdns.pojo.entity.SystemRole;
import com.lwc.cfdns.pojo.vo.system.RoleVO;
import com.lwc.cfdns.service.SystemAdminRoleService;
import com.lwc.cfdns.service.SystemRoleService;
import com.lwc.cfdns.mapper.SystemRoleMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_role(角色信息表)】的数据库操作Service实现
 * @createDate 2023-05-23 15:18:02
 */
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRole>
        implements SystemRoleService {
    @Autowired
    private SystemRoleMenuServiceImpl systemRoleMenuService;

    @Autowired
    private SystemRoleDeptServiceImpl systemRoleDeptService;

    @Autowired
    private SystemAdminRoleService systemAdminRoleService;

    /**
     * 创建
     *
     * @param roleDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/23 15:30:23
     */
    @Override
    @Transactional
    public Map<String, Object> create(RoleDTO roleDTO) {
        Map<String, Object> map = new HashMap<>();
        SystemRole systemRole = new SystemRole();
        BeanUtils.copyProperties(roleDTO, systemRole);
        this.baseMapper.insert(systemRole);
        //权限处理
        systemRoleMenuService.rules(systemRole.getId(), roleDTO.getRules());
        map.put("id", systemRole.getId());
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return RoleVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/23 15:30:43
     */
    @Override
    public RoleVO view(Long id) {
        RoleVO roleVO = new RoleVO();
        SystemRole systemRole = this.baseMapper.selectById(id);
        BeanUtils.copyProperties(systemRole, roleVO);
        return roleVO;
    }

    /**
     * 更新
     *
     * @param id
     * @param roleDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/23 15:33:31
     */
    @Override
    @Transactional
    public Map<String, Object> update(Long id, RoleDTO roleDTO) {
        HashMap<String, Object> map = new HashMap<>();
        SystemRole systemRole = this.baseMapper.selectById(id);
        Assert.notNull(systemRole, ResultCode.NOT_DATA_FOUNT);
        BeanUtils.copyProperties(roleDTO, systemRole, "created_at");
        systemRole.setId(id);
        int i = this.baseMapper.updateById(systemRole);
        // 权限处理
        systemRoleMenuService.rules(systemRole.getId(), roleDTO.getRules());
        map.put("result", i);
        return map;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/23 15:33:59
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemRole>().in(SystemRole::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param roleBO
     * @return IPage<SystemRole>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/23 15:38:50
     */
    @Override
    public IPage<SystemRole> lists(RoleBO roleBO) {
        return this.baseMapper.selectPage(roleBO.toPage(), roleBO.toQueryWrapper());
    }

    /**
     * 树
     *
     * @param roleBO
     * @return List<RoleVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/23 16:05:42
     */
    @Override
    public List<Map<String, Object>> tree(RoleBO roleBO) {
        List<SystemRole> roles = this.baseMapper.selectList(roleBO.toQueryWrapper());
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (SystemRole role : roles) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", role.getId());
            map.put("name", role.getName());
            list.add(map);
        }
        return list;
    }

    /**
     * 状态修改
     *
     * @param changeStatusDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 16:01
     */
    @Override
    public Map<String, Object> changeStatus(ChangeStatusDTO changeStatusDTO) {
        SystemRole systemRole = this.baseMapper.selectById(changeStatusDTO.getId());
        Assert.notNull(systemRole, ResultCode.NOT_DATA_FOUNT);
        LambdaUpdateWrapper<SystemRole> objectUpdateWrapper = new LambdaUpdateWrapper<>();
        objectUpdateWrapper.eq(SystemRole::getId, changeStatusDTO.getId());
        objectUpdateWrapper.set(SystemRole::getStatus, changeStatusDTO.getStatus());
        int i = this.baseMapper.update(null, objectUpdateWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 数据权限
     *
     * @param roleDataScopeDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 10:16:24
     */
    @Transactional
    public Map<String, Object> dataScope(RoleDataScopeDTO roleDataScopeDTO) {
        SystemRole systemRole = this.baseMapper.selectById(roleDataScopeDTO.getId());
        Assert.notNull(systemRole, ResultCode.NOT_DATA_FOUNT);
        LambdaUpdateWrapper<SystemRole> objectUpdateWrapper = new LambdaUpdateWrapper<>();
        objectUpdateWrapper.eq(SystemRole::getId, roleDataScopeDTO.getId());
        objectUpdateWrapper.set(SystemRole::getDeptCheckStrictly, roleDataScopeDTO.getDeptCheckStrictly());
        objectUpdateWrapper.set(SystemRole::getDataScope, roleDataScopeDTO.getDataScope());
        int i = this.baseMapper.update(null, objectUpdateWrapper);
        // 处理关联表
        systemRoleDeptService.deptIds(roleDataScopeDTO.getDeptIds(), roleDataScopeDTO.getId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 批量给角色添加授权用户
     *
     * @param roleAuthUserDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 13:48:15
     */
    @Override
    public Map<String, Object> addAuthUser(RoleAuthUserDTO roleAuthUserDTO) {
        SystemRole systemRole = this.baseMapper.selectById(roleAuthUserDTO.getRoleId());
        Assert.notNull(systemRole, ResultCode.NOT_DATA_FOUNT);
        // 批量给角色添加管理员授权
        boolean b = systemAdminRoleService.batAddAdminIdsOfRoles(roleAuthUserDTO.getRoleId(), roleAuthUserDTO.getAdminIds());
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", b);
        return map;
    }

    /**
     * 批量给角色取消授权用户
     *
     * @param roleAuthUserDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 13:49:34
     */
    @Override
    public Map<String, Object> cancelAuthUser(RoleAuthUserDTO roleAuthUserDTO) {
        SystemRole systemRole = this.baseMapper.selectById(roleAuthUserDTO.getRoleId());
        Assert.notNull(systemRole, ResultCode.NOT_DATA_FOUNT);
        // 批量给角色取消管理员授权
        boolean b = systemAdminRoleService.batCancelAdminIdsOfRoles(roleAuthUserDTO.getRoleId(), roleAuthUserDTO.getAdminIds());
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", b);
        return map;
    }

    /**
     * 用户角色
     *
     * @param admin_id
     * @return List<RoleVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 17:13:53
     */
    public List<RoleVO> getRoleByUserId(Long admin_id) {
        return this.baseMapper.getRoleByUserId(admin_id);
    }
}




